package newcoder.JZ63;

//[8,9,2,5,4,7,1]
public class Solution {

    public static void main(String[] args) {
        int[] prices = {8, 9, 2, 5, 4, 7, 1};
        System.out.println(maxProfit(prices));
    }

    public static int maxProfit(int[] prices) {
        // write code here
//        int max = 0;
//        for (int i = 1; i < prices.length; i++) {
//            if (prices[i]>prices[i-1]){
//                max+=(prices[i]-prices[i-1]);
//            }
//        }
//        return max;

        int min = prices[0];
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i]-min>profit){
                profit = prices[i]-min;
            }
            else if(prices[i]<min){
                min = prices[i];
            }
        }
        return profit;
    }
}
